//const修饰指针

#include<iostream>
#include<string.h>

using namespace std;
int main()
{
	//1.const 修饰指针 变量指针
	int a = 10;
	int b = 20;
	const int* p = &a;
	cout << "1.const int* p = &a 修饰指针 变量指针" << endl;
	cout << "变化前a=" << a << endl;	//a=10
	cout << "变化前b=" << b << endl;	//b=20
	cout << "变化前*p=" << *p << endl;	//*p=10
	//指针指向值不可更改，指针的指向可以更改
	//*p = 20; 错误
	p = &b;	 //正确
	cout << "a=" << a << endl;				//a=10
	cout << "b=" << b << endl;				//b=20
	cout <<"*p="<< * p << endl;//输出b的值	//*p=20
	cout << endl;

	//2.const 修饰常量 指针变量
	int* const p2 = &a;
	cout << "2.int* const p2 = &a 修饰常量 指针变量" << endl;
	cout << "变化前a=" << a << endl;	//a=10
	cout << "变化前b=" << b << endl;	//b=20
	cout << "变化前*p2=" << *p << endl;	//*p=10
	//指针的指向值可以更改,指针的指向不可更改
	*p2 = 100; //正确
	//p2 = &b;   错误
	cout << "a=" << a << endl;			//a=100
	cout << "b=" << b << endl;			//b=20
	cout << "*p2=" << *p2<< endl;		//*p=100
	cout << endl;
	//3.const 修饰指针和常量
	const int* const p3 = &a;
	//指针和指向都不可以改
	//*p3 = 100;错误
	//p3 = &b;错误
	system("pause");
	return 0;
}